#==============================================================================
# ** Sprite_Battle_GTBS
#------------------------------------------------------------------------------
#  This sprite is used to display characters. It observes a instance of the
# Game_Actor/Enemy class and automatically changes sprite conditions.
#==============================================================================

class Sprite_Battler_GTBS < Turn_Sprite
  #--------------------------------------------------------------------------
  # * Constants
  #--------------------------------------------------------------------------
  WHITEN    = 1                      # Flash white (start action)
  BLINK     = 2                      # Blink (damage)
  APPEAR    = 3                      # Appear (appear, revive)
  DISAPPEAR = 4                      # Disappear (escape)
  COLLAPSE  = 5                      # Collapse (incapacitated)
  BALLOON_WAIT = 12                  # Final balloon frame wait time
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :battler
  attr_reader   :_damage_duration
  attr_reader   :_animation_duration 
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     viewport  : viewport
  #     battler   : character (Game_Battler)
  #--------------------------------------------------------------------------
  def initialize(viewport, battler = nil)
    super(viewport)
    @battler = battler
    @balloon_duration = 0
    @effect_duration = 0
    @battler_visible = false
    @down            = false
    @pose_started    = false
    @anim            = false
    @last_time, @frame_index, @pose = 0, 0, 0
    @loop_type = GTBS::DEFAULT_POSE_NUMBERS['Wait'] + 
      GTBS::DEFAULT_POSE_NUMBERS['Walk'] + GTBS::DEFAULT_POSE_NUMBERS['Defend'] + 
      GTBS::DEFAULT_POSE_NUMBERS['Dead'] + GTBS::DEFAULT_POSE_NUMBERS['Near Death']
  end
  #----------------------------------------------------------------------------
  # Bat - this method returns the battler that is held in this object
  #----------------------------------------------------------------------------
  def bat
    return @battler
  end
  #----------------------------------------------------------------------------
  # Dispose Process
  #----------------------------------------------------------------------------
  def dispose
    dispose_bitmap
    dispose_balloon
    super
  end
  #----------------------------------------------------------------------------
  # Dispose Bitmap - erases image
  #----------------------------------------------------------------------------
  def dispose_bitmap
    if self.bitmap != nil
      self.bitmap.dispose
    end
  end
  #----------------------------------------------------------------------------
  # * Update
  #----------------------------------------------------------------------------
  def update
    super
    if @battler == nil and !GTBS::VX
      self.bitmap = nil
      loop_animation(nil)
      return
    end
    update_bitmap
    update_state_animation if !GTBS::VX
    update_opacity
    update_blink
    update_check_hidden
    if @battler_visible
      if @battler.hidden and !GTBS::VX
        Sound.play_escape
        escape
        @battler_visible = false
      end
      update_white_flash
      update_state_pose
      check_animations
      check_damage_pop
      check_collapse
    end
    update_pose
    self.x = @battler.screen_x
    self.y = @battler.screen_y
    self.z = @battler.screen_z(@ch)
    offset_large_unit
    self.blend_type = @battler.blend_type
    self.bush_depth = @battler.bush_depth
    if GTBS::VX
      update_balloon
      if @battler.balloon_id != 0
        @balloon_id = @battler.balloon_id
        start_balloon
        @battler.balloon_id = 0
      end
      setup_new_effect
      update_effect
    end
  end
  #----------------------------------------------------------------------------
  # Offset Large Unit - Sprite
  #----------------------------------------------------------------------------
  def offset_large_unit
    #large unit position update
    if !$game_map.iso?
      self.x += 16*(@battler.unit_size-1)
      self.y += 16*(@battler.unit_size-1)
    else
      self.y += 16*(@battler.unit_size-1)
    end
  end
  
  #--------------------------------------------------------------------------
  # * Set New Effect
  #--------------------------------------------------------------------------
  def setup_new_effect
    return if @effect_duration > 0
    if @battler.white_flash
      @effect_type = WHITEN
      @effect_duration = 16
      @battler.white_flash = false
    end
    if @battler.blink
      @effect_type = BLINK
      @effect_duration = 80
      #@battler.blink = false
    end
    if not @battler_visible and @battler.exist?
      @effect_type = APPEAR
      @effect_duration = 16
      @battler_visible = true
    end
    if @battler_visible and @battler.hidden
      @effect_type = DISAPPEAR
      @effect_duration = 32
      @battler_visible = false
    end
    if @battler.collapse
      @effect_type = COLLAPSE
      @effect_duration = 48
      @battler.collapse = false
      @battler_visible = false
    end
    if @battler.animation_id != 0
      animation = $data_animations[@battler.animation_id]
      mirror = @battler.animation_mirror
      start_animation(animation, mirror)
      @battler.animation_id = 0
    end
  end
  #--------------------------------------------------------------------------
  # * Update Effect
  #--------------------------------------------------------------------------
  def update_effect
    if @effect_duration > 0
      @effect_duration -= 1
      case @effect_type
      when WHITEN
        update_whiten
      when BLINK
        update_blink
      when APPEAR
        update_appear
      when DISAPPEAR
        update_disappear
      when COLLAPSE
        update_collapse
        if @effect_duration == 0
          @battler.transparent = true
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Update White Flash Effect
  #--------------------------------------------------------------------------
  def update_whiten
    self.blend_type = 0
    self.color.set(255, 255, 255, 128)
    self.opacity = 255
    self.color.alpha = 128 - (16 - @effect_duration) * 10
  end
  #--------------------------------------------------------------------------
  # * Update Blink Effect
  #--------------------------------------------------------------------------
  def update_blink
    self.blend_type = 0
    self.color.set(255, 255, 255, 128)
    self.opacity = 255
    self.color.alpha = 128 - (16 - (@effect_duration/4)) * 10
    #self.visible = (@effect_duration % 20 > 5)
  end
  #--------------------------------------------------------------------------
  # * Update Appearance Effect
  #--------------------------------------------------------------------------
  def update_appear
    self.blend_type = 0
    self.color.set(0, 0, 0, 0)
    self.opacity = (16 - @effect_duration) * 16
  end
  #--------------------------------------------------------------------------
  # * Updated Disappear Effect
  #--------------------------------------------------------------------------
  def update_disappear
    self.blend_type = 0
    self.color.set(0, 0, 0, 0)
    self.opacity = 256 - (32 - @effect_duration) * 10
  end
  #--------------------------------------------------------------------------
  # * Update Collapse Effect
  #--------------------------------------------------------------------------
  def update_collapse
    self.blend_type = 1
    self.color.set(255, 128, 128, 128)
    self.opacity = 256 - (48 - @effect_duration) * 6
  end
  
  def check_damage_pop
    if @battler.damage2_pop == true
      doom_pop(@battler.damage2)
      @battler.damage2 = nil
      @battler.damage2_pop = false
    end
    if @battler.damage_pop == true
      if @battler.damage == "Evade"
        @battler.dodge
        @battler.damage = "Miss"
      end
      damage(@battler.damage, @battler.critical)
      if @battler.damage.is_a?(Numeric)
        if @battler.damage < 0
          @battler.set_pose("heal") unless !GTBS::HEAL_POSE
        else
          @battler.set_pose("pain") 
        end
      else
        if @battler.damage == "Miss"
          @battler.set_pose("defend")
        elsif @battler.damage.include?("CRITICAL") or @battler.damage.include?('DOOM!')
          @battler.set_pose("pain") 
        else  
          @battler.set_pose("heal") unless !GTBS::HEAL_POSE
        end
      end
      @battler.damage = nil
      @battler.critical = false
      @battler.damage_pop = false
    end
  end 
  
  def update_death
    if @battler.damage == nil and @battler.dead? and !@battler.collapsed?
      if ![4,5].include?(@battler.pose?)#(pain or collapsing... )
        if GTBS::REMOVE_DEAD
          @battler.perform_collapse
          @battler.collapsed = true
          @battler_visible = false
        else
          @battler.collapsed = true
          @battler.set_pose("collapse")
          if @battler.is_a?(Game_Enemy)
            Sound.play_enemy_collapse
          else
            Sound.play_actor_collapse
          end
        end
      end
    end
  end
  
  def update_pose
    if @battler.animation_id != 0
      if @battler_anim
        case @battler.animation_id
        when GTBS::ANIM_ATK
          @battler.set_pose("attack")
          @battler.animation_id = 0
        when GTBS::ANIM_SPEC1
          @battler.set_pose("spec1")
          @battler.animation_id = 0
        when GTBS::ANIM_SPEC2
          @battler.set_pose("spec2")
          @battler.animation_id = 0
        when GTBS::ANIM_CASTING
          @battler.set_pose("casting")
          @battler.animation_id = 0
        when GTBS::ANIM_CAST
          @battler.set_pose("cast")
          @battler.animation_id = 0
        when GTBS::ANIM_HEAL
          @battler.set_pose("heal")
          @battler.animation_id = 0
        else
          animation = $data_animations[@battler.animation_id]
          mirror = @battler.animation_mirror
          start_animation(animation,mirror,@battler.direction)
          @battler.animation_id = 0
        end
      else
        animation = $data_animations[@battler.animation_id]
        mirror = @battler.animation_mirror
        start_animation(animation,mirror,@battler.direction)
        @battler.animation_id = 0
      end
    end
  end
  
  #--------------------------------------------------------------------------
  # * Get tile set image that includes the designated tile
  #     tile_id : Tile ID
  #--------------------------------------------------------------------------
  def tileset_bitmap(tile_id)
    set_number = tile_id / 256
    return Cache.system("TileB") if set_number == 0
    return Cache.system("TileC") if set_number == 1
    return Cache.system("TileD") if set_number == 2
    return Cache.system("TileE") if set_number == 3
    return nil
  end
  #----------------------------------------------------------------------------
  # Update Bitmap - Updates the battler info and sprite info
  #----------------------------------------------------------------------------
  def update_bitmap
    if @battler_name != @battler.character_name or
       @battler_index != @battler.character_index or @battler_hue != @battler.battler_hue 
      @battler_name = @battler.character_name
      @battler_hue = @battler.battler_hue
      @battler_index = @battler.character_index
      @battler_anim = @battler.animated?
      #create bitmap
      self.bitmap = Cache.battler(@battler_name,@battler_hue)
      sign = @battler_name[/^[\!\$]./]
      if sign != nil and sign.include?('$')
        if !@battler_anim
          @cw = bitmap.width / 3
          @ch = bitmap.height / 4
        else
          @cw = bitmap.width / 6
          @ch = bitmap.height / 11 / 4
        end
      else
        if !@battler_anim
          @cw = bitmap.width / 12
          @ch = bitmap.height / 8
        else
          @cw = bitmap.width / 18
          @ch = bitmap.height / 11 / 8
        end
      end
      self.ox = @cw / 2
      self.oy = @ch
    end
  end
  #--------------------------------------------------------------------------
  # * Update Transfer Origin Rectangle
  #--------------------------------------------------------------------------
  def update_src_rect
    index = @battler.character_index
    if !@battler_anim
      pattern = @battler.pattern < 3 ? @battler.pattern : 1
    else
      pattern = @battler.pattern < 5 ? @battler.pattern : 1
    end
    
    if !@battler_anim
      sx = (index % 4 * 3 + pattern) * @cw
      sy = (index / 4 * 4 + (@battler.direction - 2) / 2) * @ch
    else
      multiplier = @battler.pose * 4
      sx = (index % 4 * 6 + pattern) * @cw
      sy = ((index / 4 * 4 + (@battler.direction - 2) / 2) * @ch) + (multiplier*@ch)
    end
    self.src_rect.set(sx, sy, @cw, @ch)
  end
  #--------------------------------------------------------------------------
  # * Start Balloon Icon Display
  #--------------------------------------------------------------------------
  def start_balloon
    dispose_balloon
    @balloon_duration = 8 * 8 + BALLOON_WAIT
    @balloon_sprite = ::Sprite.new(viewport)
    @balloon_sprite.bitmap = Cache.system("Balloon")
    @balloon_sprite.ox = 16
    @balloon_sprite.oy = 32
    update_balloon
  end
  #--------------------------------------------------------------------------
  # * Update Balloon Icon
  #--------------------------------------------------------------------------
  def update_balloon
    if @balloon_duration > 0
      @balloon_duration -= 1
      if @balloon_duration == 0
        dispose_balloon
      else
        @balloon_sprite.x = x
        @balloon_sprite.y = y - height
        @balloon_sprite.z = z + 200
        if @balloon_duration < BALLOON_WAIT
          sx = 7 * 32
        else
          sx = (7 - (@balloon_duration - BALLOON_WAIT) / 8) * 32
        end
        sy = (@balloon_id - 1) * 32
        @balloon_sprite.src_rect.set(sx, sy, 32, 32)
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Dispose of Balloon Icon
  #--------------------------------------------------------------------------
  def dispose_balloon
    if @balloon_sprite != nil
      @balloon_sprite.dispose
      @balloon_sprite = nil
    end
  end
end

class Game_Character
  def _damage_duration
    return 0
  end
  def damage_pop
    return false
  end
  def damage2_pop
    return false
  end
end

  